package cn.lopr.im.android.mvp.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.IAddFriendContact;
import cn.lopr.im.android.utils.Apis;
import cn.lopr.im.android.utils.GsonUtil;
import cn.lopr.im.android.utils.HttpUtils;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class AddFriendModel implements IAddFriendContact.IAddFriendModel {

    @Override
    public void searchFriend(String token, String content, OnCallBackListener<CallbackResult<List<User>>, String> onCallBackListener) {
        String param = String.format("?content=%s", content);
        HttpUtils.doGetForResult(Apis.WebApis.SEARCH_FRIEND.getValue(), param, token, data -> {
            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<User>>>() {
            }.getType();
            CallbackResult<List<User>> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getFriendByPhones(String token, List<String> users, OnCallBackListener<CallbackResult<List<User>>, String> onCallBackListener) {
        String param = "{\"phones\":" + GsonUtil.TTOString(users) + "}";
        Log.i("sdsd", "getFriendByPhones: " + param);
        HttpUtils.doPostForResult(Apis.WebApis.GET_FRIEND_BY_PHONES.getValue(), param, token, data -> {
            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<User>>>() {
            }.getType();
            CallbackResult<List<User>> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void addFriend(String token, String content, OnCallBackListener<CallbackResult<List<User>>, String> onCallBackListener) {

        String param = String.format("?content=%s", content);
        HttpUtils.doGetForResult(Apis.WebApis.ADD_FRIEND.getValue(), param, token, data -> {

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<User>>>() {
            }.getType();
            CallbackResult<List<User>> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

}
